/*
 * UserService.java
 * Copyright (c) 2014, 2019, Kiri. 
 * All rights reserved.
 */
package com.onesalt.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import com.onesalt.domain.Kitchen;
import com.onesalt.domain.User;
import com.onesalt.utils.NumberUtil;

/**
 * UserService
 *
 * @author kiri
 * @date 2015年4月8日
 * @version 1.0
 */
@Service
@Transactional
public class UserService extends AbstractService {

	private final UserRepository userRepository;

	private final KitchenRepository kitchenRepository;

	@Autowired
	public UserService(UserRepository userRepository, KitchenRepository kitchenRepository) {
		this.userRepository = userRepository;
		this.kitchenRepository = kitchenRepository;
	}

	public Page<User> findAllCooks(String sn, String name, String city, String tel, String mobile, Long kitchenId, String kitchenName,
			Integer startAge, Integer endAge, Date startApplyDate, Date endApplyDate, Date startRegisterDate, Date endRegisterDate,
			String notes, int page, int size) {
		List<SearchFilter> filters = new ArrayList<SearchFilter>();
		if (StringUtils.hasText(sn)) {
			filters.add(new SearchFilter("sn", Operator.LIKE, sn));
		}
		if (StringUtils.hasText(tel)) {
			filters.add(new SearchFilter("tel", Operator.LIKE, tel));
		}
		if (kitchenId != null) {
			filters.add(new SearchFilter("kitchen.kitchenId", Operator.LIKE, kitchenId));
		}
		if (StringUtils.hasText(kitchenName)) {
			filters.add(new SearchFilter("kitchen.name", Operator.LIKE, kitchenName));
		}
		if (StringUtils.hasText(name)) {
			filters.add(new SearchFilter("name", Operator.LIKE, name));
		}
		if (StringUtils.hasText(city)) {
			filters.add(new SearchFilter("city", Operator.LIKE, city));
		}
		if (StringUtils.hasText(mobile)) {
			filters.add(new SearchFilter("mobile", Operator.LIKE, mobile));
		}
		if (startAge != null) {
			filters.add(new SearchFilter("age", Operator.GTE, startAge));
		}
		if (endAge != null) {
			filters.add(new SearchFilter("age", Operator.LT, endAge));
		}
		if (startApplyDate != null) {
			filters.add(new SearchFilter("cook.applyTime", Operator.GTE, startApplyDate));
		}
		if (endApplyDate != null) {
			filters.add(new SearchFilter("cook.applyTime", Operator.LT, endApplyDate));
		}
		if (startRegisterDate != null) {
			filters.add(new SearchFilter("registerTime", Operator.GTE, startRegisterDate));
		}
		if (endRegisterDate != null) {
			filters.add(new SearchFilter("registerTime", Operator.LT, endRegisterDate));
		}
		if (StringUtils.hasText(notes)) {
			filters.add(new SearchFilter("notes", Operator.LIKE, notes));
		}
		filters.add(new SearchFilter("cook", Operator.NOTNULL, null));

		return getUserRepository().findAll(DynamicSpecifications.bySearchFilter(filters, User.class), new PageRequest(page, size));
	}

	public List<String> findAllCities() {
		return getUserRepository().findAllCities();
	}

	public User findOrCreateUserByMobile(String mobile) {
		User user = getUserRepository().findByMobile(mobile);
		if (user != null) {
			return user;
		}
		user = new User();
		user.setMobile(mobile);
		user.setCreateTime(new Date());
		user = getUserRepository().save(user);
		user.setSn("CHN" + user.getUserId());
		user.setUsername(user.getSn());
		return getUserRepository().save(user);
	}

	public UserRepository getUserRepository() {
		return userRepository;
	}

	public KitchenRepository getKitchenRepository() {
		return kitchenRepository;
	}

	public User createCook(User cook, Kitchen kitchen) {
		cook = getUserRepository().save(cook);
		kitchen.setCook(cook);
		kitchen.setSn(cook.getSn() + NumberUtil.format(kitchen.getKitchenId(), 3));
		kitchen = getKitchenRepository().save(kitchen);
		return cook;
	}

	public User auditCook(long userId, String auditStatus) {
		User cook = getUserRepository().findOne(userId);
		if (cook == null) {
			return null;
		}
		if (auditStatus != null && auditStatus.equals(cook.getCookAuditStatus())) {
			return cook;
		}
		if (User.CookAuditStatus.PASS.equals(auditStatus)) {
			User lastPassedCook = getUserRepository().findTopByCookAuditStatus(auditStatus, new Sort(Direction.DESC, "cookAuditTime"));
			if (lastPassedCook != null && lastPassedCook.getCookPassIndex() != null) {
				cook.setCookPassIndex(lastPassedCook.getCookPassIndex() + 1);
			} else {
				cook.setCookPassIndex(1L);
			}
		}
		cook.setCookAuditStatus(auditStatus);
		cook.setCookAuditTime(new Date());
		cook = getUserRepository().save(cook);
		return cook;
	}
}
